#pragma once
#include<unordered_map>
#include<fstream>
#include<string>
#include<iostream>
#include"Log.hpp"
#include"Udp_Server.hpp"
const static std::string SEP = ": ";
const static std::string gPathname = "./Dict.txt";
using namespace LOGSpace;
class Dict
{
private:
    void LoadDit()
    {
        std::ifstream in(_path);
        if(!in.is_open())
        {
            LOG(FATAL,"open dict.txt error!\n");
            exit(DICT_ERROR);
        }
        //按行读取文件
        std::string line;
        while(std::getline(in,line))
        {
            LOG(DEBUG,"load info: %s\n",line.c_str());
            if(!line.size()) continue;
            //查找:
            auto pos = line.find(SEP);
            if(pos == std::string::npos) continue;

            std::string key = line.substr(0,pos);
            std::string value = line.substr(pos+SEP.size());
            if(key.empty() || value.empty()) continue;
            // _dict[key] = value;
            _dict.insert(std::make_pair(key,value));
        }
        LOG(DEBUG,"%s load done!\n",_path.c_str());
        in.close();
    }
public:
    Dict(const std::string& path = gPathname) : _path(path)
    {
       //加载dit配置文件
       LoadDit();
    }
    std::string EngToChin(std::string word)
    {
        if(word.empty()) return "none";
        std::unordered_map<std::string,std::string>::iterator it = _dict.find(word);
        if(it == _dict.end()) return "dict not find this word !";
        return it->second;
    }
    ~Dict()
    {}
private:
    std::unordered_map<std::string,std::string> _dict;
    std::string _path;
};